package org.extframework.ld.content;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.extframework.ld.conf.XmlConfigName;
import org.extframework.ld.content.model.XmlMenuNode;
import org.extframework.ld.exception.WebException;
import org.extframework.ld.util.FileUtil;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.springframework.core.io.ClassPathResource;

/**
 * @author : Dong.Li
 * @created : 2013-1-17
 * @version : 1.0
 * @description :
 */
@SuppressWarnings("unchecked")
public class XmlContent {

	private static Element rootElement;
	private static Element extElement;
	private static Map<String, XmlMenuNode> nodes;
	private static List<XmlMenuNode> rootMenuNode;
	private static File XMLFile;

	static {
		initXML();
	}

	private static void initXML() {
		nodes = new HashMap<String, XmlMenuNode>();
		rootMenuNode = new ArrayList<XmlMenuNode>();
		SAXBuilder builder = new SAXBuilder();
		try {
			URI url = Thread.currentThread().getContextClassLoader()
					.getResource("web.ext.xml").toURI();

			XMLFile = new File(url);

			Document doc = builder.build(XMLFile);
			rootElement = doc.getRootElement();
			extElement = rootElement.getChild("Ext");
			initIng();
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void initIng() throws JDOMException {
		SAXBuilder builder = new SAXBuilder();
		Document doc = builder
				.build(new File(getExtMenu(), XmlConfigName.menu));
		Element ele = doc.getRootElement();

		XmlMenuNode node = new XmlMenuNode();
		parseMenu(ele, node);
	}

	private static void parseMenu(Element ele, XmlMenuNode node) {
		System.gc();

		List<Element> tempItem = ele.getChildren("item");
		XmlMenuNode chiledItemNode;
		for (Element chiledItem : tempItem) {
			chiledItemNode = new XmlMenuNode();
			chiledItemNode.setId(chiledItem.getAttributeValue("id"));
			chiledItemNode.setText(chiledItem.getAttributeValue("title"));
			chiledItemNode.setPageId(chiledItem.getAttributeValue("pageId"));
			chiledItemNode.setIconCls(chiledItem.getAttributeValue("iconCls"));
			if(chiledItem.getAttributeValue("sort") != null)
				chiledItemNode.setSort(Integer.parseInt(chiledItem.getAttributeValue("sort")));

			chiledItemNode.setLeaf(true);
			chiledItemNode.setParentMenuNode(node);
			node.addChiled(chiledItemNode);
			addRootMenuNode(chiledItemNode);
			try {
				ExtContent.addMenuPage(chiledItemNode.getId(),
						chiledItemNode.getPageId());
			} catch (WebException e) {
				e.printStackTrace();
			}
		}

		List<Element> tempMenu = ele.getChildren("menu");
		for (Element chiledMenu : tempMenu) {
			XmlMenuNode chiledMenuNode = new XmlMenuNode();
			chiledMenuNode.setId(chiledMenu.getAttributeValue("id"));
			chiledMenuNode.setText(chiledMenu.getAttributeValue("title"));
			chiledMenuNode.setIconCls(chiledMenu.getAttributeValue("iconCls"));
			if(chiledMenu.getAttributeValue("sort") != null)
				chiledMenuNode.setSort(Integer.parseInt(chiledMenu.getAttributeValue("sort")));
			
			chiledMenuNode.setParentMenuNode(node);
			chiledMenuNode.setLeaf(false);
			node.addChiled(chiledMenuNode);
			addNode(chiledMenuNode);
			parseMenu(chiledMenu, chiledMenuNode);
		}
	}

	public static void addRootMenuNode(XmlMenuNode menuNode) {
		if ((menuNode.getParentMenuNode() != null)
				&& (menuNode.getParentMenuNode().getId() != null)
				&& (menuNode.getParentMenuNode().getText() != null))
			return;
		rootMenuNode.add(menuNode);
	}

	public static XmlMenuNode[] getRootNodes() {
		return ((XmlMenuNode[]) rootMenuNode.toArray(new XmlMenuNode[0]));
	}
	
	public static void addNode(XmlMenuNode menuNode) {
		addRootMenuNode(menuNode);
		nodes.put(menuNode.getId(), menuNode);
	}

	public static XmlMenuNode getNode(String menuId) {
		return ((XmlMenuNode) nodes.get(menuId));
	}

	public static String getDatabaseKey() {
		return rootElement.getChild("Des").getAttributeValue("database-key");
	}
	
	public static String getRootUrl(){
		String url = rootElement.getChild("rootUrl").getTextTrim();
		return url;
	}

	public static String getLocale() {
		String dir = rootElement.getChild("Locale").getAttributeValue(
				"location-dir");
		File file = new File(XMLFile.getParent(), dir);
		FileUtil.isExist(file, true);
		return file.getPath();
	}

	public static String getExtDetail() {
		String dir = extElement.getChild("detail").getAttributeValue(
				"location-dir");
		File file = new File(XMLFile.getParent(), dir);
		FileUtil.isExist(file, true);
		return file.getPath();
	}

	public static String getExtMenu() {
		String dir = extElement.getChild("menu").getAttributeValue(
				"location-dir");
		File file = new File(XMLFile.getParent(), dir);
		FileUtil.isExist(file, true);
		return file.getPath();
	}

	public static String getExtPage() {
		String dir = extElement.getChild("page").getAttributeValue(
				"location-dir");
		File file = new File(XMLFile.getParent(), dir);
		FileUtil.isExist(file, true);
		return file.getPath();
	}

	public static String getExtDialog() {
		String dir = extElement.getChild("dialog").getAttributeValue(
				"location-dir");
		File file = new File(XMLFile.getParent(), dir);
		FileUtil.isExist(file, true);
		return file.getPath();
	}

	public static String getExtCustom() {
		String dir = extElement.getChild("custom").getAttributeValue(
				"location-dir");
		File file = new File(XMLFile.getParent(), dir);
		FileUtil.isExist(file, true);
		return file.getPath();
	}

	public static String getExtProcess() {
		String dir = extElement.getChild("process").getAttributeValue(
				"location-dir");
		try {
			FileUtil.isExist(new ClassPathResource(dir).getFile(), true);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return dir;
	}

}
